package com.life.wd.controller;

import com.life.wd.dto.CommunityPostAddRequest;
import com.life.wd.dto.CommunityPostQueryRequest;
import com.life.wd.entity.CommunityPosts;
import com.life.wd.result.PageResult;
import com.life.wd.result.Result;
import com.life.wd.service.CommunityPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/community/posts")
@Slf4j
@Api(tags = "社区帖子管理")
public class CommunityPostController {

    @Autowired
    private CommunityPostService communityPostService;

    /**
     * 添加帖子
     */
    @PostMapping("/add")
    @ApiOperation("添加帖子")
    public Result<Integer> addPost(@Valid @ModelAttribute CommunityPostAddRequest request) {
        log.info("添加帖子: {}", request);
        try {
            Integer postId = communityPostService.addPost(request);
            return Result.success(postId);
        } catch (Exception e) {
            return Result.error("添加帖子失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询帖子列表
     */
    @GetMapping("/list")
    @ApiOperation("分页查询帖子列表")
    public Result listPosts(CommunityPostQueryRequest request) {
        PageResult result = communityPostService.listPosts(request);
        return Result.success(result);
    }

    /**
     * 根据ID获取帖子详情
     */
    @GetMapping("/{postId}")
    @ApiOperation("获取帖子详情")
    public Result<CommunityPosts> getPostById(@PathVariable Integer postId) {
        CommunityPosts post = communityPostService.getPostById(postId);
        return Result.success(post);
    }

    /**
     * 删除单个帖子
     */
    @DeleteMapping("/{postId}")
    @ApiOperation("删除单个帖子")
    public Result<Boolean> deletePost(@PathVariable Integer postId) {
        try {
            boolean success = communityPostService.deletePost(postId);
            return Result.success(success);
        } catch (Exception e) {
            return Result.error("删除帖子失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除帖子
     */
    @DeleteMapping("/batch")
    @ApiOperation("批量删除帖子")
    public Result<Boolean> deletePostsBatch(@RequestBody List<Integer> postIds) {
        try {
            boolean success = communityPostService.deletePostsBatch(postIds);
            return Result.success(success);
        } catch (Exception e) {
            return Result.error("批量删除帖子失败: " + e.getMessage());
        }
    }
}